package com.pipayshop.controller;

import com.pipayshop.domain.vo.FansVO;
import com.pipayshop.entity.vo.ResponseVO;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.OnlineStoreShopFollowFocusSerivce;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaoqiu
 * @date 2024/9/23 10:35
 * @description 网店粉丝关注接口
 */
@RestController
@RequestMapping("/pipayshopapi/followfocus")
@Slf4j
public class OnlineStoreShopFollowFocusController {

    @Autowired
    private OnlineStoreShopFollowFocusSerivce followFocusService;
    @PostMapping("userFollowShop/{followId}/{shopId}")
    @ApiOperation("关注或取消网店")
    public ResponseVO userFollowShop(@PathVariable("followId") String followId, @PathVariable("shopId") String shopId) {
        try {
            Boolean update = followFocusService.userFollowShop(followId, shopId);
            if (!update ){
                return ResponseVO.getSuccessResponseVo("unfollow_store_success");
            }
            return ResponseVO.getSuccessResponseVo("follow_store_success");
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("operation_failed");
        }
    }

    @GetMapping("shopFans/{shopId}")
    @ApiOperation("网店粉丝列表")
    private ResponseVO ShopFans(@PathVariable("shopId") String shopId, @RequestParam(defaultValue = "0") Integer pageNum , @RequestParam(defaultValue = "10") Integer pageSize){
        try {
            List<FansVO> result = followFocusService.shopFans(shopId,pageNum,pageSize);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_fans_list_failed");
        }
    }

    /**
     * 查询网店粉丝总数接口
     * @return
     */
    @GetMapping("shopFansSum/{shopId}")
    @ApiOperation("查询网店粉丝总数接口")
    private ResponseVO shopFansSum(@PathVariable String shopId){
        try {
            Long result = followFocusService.shopFansSum(shopId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("get_fans_count_failed");
        }
    }
    /**
     * 校验是否已经关注该店铺
     */
    @GetMapping("isFollowShop/{shopId}/{userId}")
    @ApiOperation("校验是否已经关注该店铺")
    public ResponseVO<Boolean> isFollowShop(@PathVariable String shopId, @PathVariable String userId){
        try{
            boolean result = followFocusService.isFollowShop(shopId,userId);
            return ResponseVO.getSuccessResponseVo(result);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new BusinessException("check_follow_store_failed");
        }
    }

}
